<template>
  <div class="search">
    <Card>
          <Row align="middle" justify="space-between" class="operation">
            <div>
              <Button @click="delAll" icon="md-add">批量下线</Button>
              <Input
                v-model="searchKey"
                suffix="ios-search"
                @on-change="search"
                placeholder="输入搜索在线用户"
                clearable
                style="width: 250px"
              />
            </div>
            <div class="icons">
              <Tooltip content="刷新" placement="top" transfer>
                <Icon
                  type="md-refresh"
                  size="18"
                  class="item"
                  @click="getDataList"
                />
              </Tooltip>
              <Tooltip
                :content="openTip ? '关闭提示' : '开启提示'"
                placement="top"
                transfer
              >
                <Icon
                  type="md-bulb"
                  size="18"
                  class="item tip"
                  @click="openTip = !openTip"
                />
              </Tooltip>
              <Tooltip content="表格密度" placement="top" transfer>
                <Dropdown @on-click="changeTableSize" trigger="click">
                  <Icon type="md-list" size="18" class="item" />
                  <DropdownMenu slot="list">
                    <DropdownItem
                      :selected="tableSize == 'default'"
                      name="default"
                      >默认</DropdownItem
                    >
                    <DropdownItem :selected="tableSize == 'large'" name="large"
                      >宽松</DropdownItem
                    >
                    <DropdownItem :selected="tableSize == 'small'" name="small"
                      >紧密</DropdownItem
                    >
                  </DropdownMenu>
                </Dropdown>
              </Tooltip>
              <Tooltip content="导出数据" placement="top" transfer>
                <Icon
                  type="md-download"
                  size="18"
                  class="item"
                  @click="exportData"
                />
              </Tooltip>
            </div>
          </Row>
          <Alert show-icon v-show="openTip">
            已选择
            <span class="select-count">{{ selectList.length }}</span> 项
            <a class="select-clear" @click="clearSelectAll">清空</a>
          </Alert>
          <Table
            :loading="loading"
            border
            :columns="columns"
            :data="data"
            :size="tableSize"
            ref="table"
            @on-selection-change="changeSelect"
          ></Table>
          <Row type="flex" justify="end" class="page">
            <Page
              :current="pageNumber"
              :total="total"
              :page-size="pageSize"
              @on-change="changePage"
              @on-page-size-change="changePageSize"
              :page-size-opts="[10, 20, 50]"
              size="small"
              show-total
              show-elevator
              show-sizer
            ></Page>
          </Row>
    </Card>
  </div>
</template>

<script>
import {
  getOnlineUserByPage,
  logoutOnlineUser,
} from "@/api/index";
export default {
  name: "onlineUser",
  data() {
    return {
      searchKey: "",
      tableSize: "default",
      openTip: true,
      loading: true, // 表单加载状态
      searchForm: {
        // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
      },
      selectList: [], // 多选数据
      columns: [
        // 表头
        {
          type: "selection",
          width: 60,
          align: "center",
        },
        {
          type: "index",
          width: 60,
          align: "center",
        },
        {
          title: "在线用户账号",
          key: "username",
          minWidth: 150,
          sortable: true,
        },
        {
          title: "IP",
          key: "ip",
          width: 180,
          tooltip: true,
          sortable: true,
        },
        {
          title: "IP信息",
          key: "ipInfo",
          width: 180,
          sortable: true,
          tooltip: true,
        },
        {
          title: "设备信息",
          key: "device",
          width: 280,
          sortable: true,
          tooltip: true,
        },
        {
          title: "上次活跃时间",
          key: "firstActiveTime",
          width: 200,
          sortable: true,
        },
        {
          title: "最近一次活跃时间",
          key: "lastActiveTime",
          width: 200,
          sortable: true,
          sortType: "desc",
        },
        {
          title: "持续活跃时间（分钟）",
          key: "activeTime",
          width: 200,
          sortable: true,
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          fixed: "right",
          width: 120,
          render: (h, params) => {
            return h("div", [
              h(
                "a",
                {
                  on: {
                    click: () => {
                      this.remove(params.row);
                    },
                  },
                },
                "强制下线"
              ),
            ]);
          },
        },
      ],
      data: [], // 表单数据
      pageNumber: 1, // 当前页数
      pageSize: 10, // 页面大小
      total: 0, // 表单数据总数
    };
  },
  methods: {
    init() {
      this.getDataList();
    },
    changeTableSize(v) {
      this.tableSize = v;
    },
    exportData() {
      this.$refs.table.exportCsv({
        filename: "数据",
      });
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    search() {
      this.searchForm.key = this.searchKey;
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    changeSelect(e) {
      this.selectList = e;
    },
    getDataList() {
      this.loading = true;
      getOnlineUserByPage(this.searchForm).then((res) => {
        this.loading = false;
        if (res.success) {
          this.data = res.result.content;
          this.total = res.result.totalElements;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      });
    },
    remove(v) {
      this.$Modal.confirm({
        title: "确认操作",
        // 记得确认修改此处
        content: "您确认要强制下线用户 " + v.username + " ?",
        loading: true,
        onOk: () => {
          // 删除
          logoutOnlineUser([v]).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.clearSelectAll();
              this.getDataList();
            }
          });
        },
      });
    },
    delAll() {
      if (this.selectList.length <= 0) {
        this.$Message.warning("您还未选择要删除的数据");
        return;
      }
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除所选的 " + this.selectList.length + " 条数据?",
        loading: true,
        onOk: () => {
          // 批量删除
          logoutOnlineUser(this.selectList).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.clearSelectAll();
              this.getDataList();
            }
          });
        },
      });
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style lang="less">
@import "@/styles/table-common.less";
</style>